var codetime = 60;

function plusReady() {
	window.addEventListener('show', function(event) {
		init();
	});
	window.addEventListener('preshow', function(event) {
		init();
	});
	
	$('.loginsendcode').click(function() {
		console.log("click");
		if($('.codemobile').val() == "") {
			mui.alert("手机号不能为空");
			return false;
		}
		console.log($('.loginsendcode').html());
		if($('.loginsendcode').html() != "发送验证码") {
			return false;
		}
		mui.get(base_url + '/api/sms/getlogincode', {
			mobile: $('.codemobile').val(),
		}, function(data) {
			console.log(data.code);
			if(data.code == 0) {
				codetime = 60;
				daojishi();
				mui.alert(data.msg);

			} else {
				mui.alert(data.msg);
			}
		}, 'json');
		return false;
	});
	$('.accountlogin').click(function(){
		var mobile = $('.accountmobile').val();
		var pwd = $('.accountpwd').val();
		if(mobile==""){
			mui.alert("手机号不能为空");
			return false;
		}
		if(pwd==""){
			mui.alert("密码不能为空");
			return false;
		}
		mui.post(base_url+'api/user/login',{
				'mobile':mobile,
				'pwd':pwd,
			},function(data){
				mui.alert(data.msg);
				if(data.code==0){
					openNewWindows('home.html');
				}
			},'json'
		);
	});
	$('.goreg').click(function(){
		openNewWindows('reg.html');
	});
	$('.gocodelogin').click(function(){
		var mobile = $('.codemobile').val();
		var code = $('.codepwd').val();
		if(mobile==""){
			mui.alert("手机号不能为空");
			return false;
		}
		if(code==""){
			mui.alert("验证码不能为空");
			return false;
		}
		mui.post(base_url+'api/user/login',{
				'mobile':mobile,
				'code':code,
			},function(data){
				mui.alert(data.msg);
				if(data.code==0){
					mui.back();
//					openNewWindows('home.html');
				}
			},'json'
		);
	});
}

function daojishi() {
	var t = codetime--;
	if(codetime > 0) {
		$('.loginsendcode').html("重新发送(" + t + ")");
		setTimeout("daojishi()", 1000);
	} else {
		$('.loginsendcode').html("发送验证码");
	}
}

function init() {
	plus.navigator.setStatusBarBackground("#27b6f0");
}
document.addEventListener("plusready", plusReady, false);